const { ipcRenderer } = require('electron')

// 获取DOM元素
const noteTitle = document.getElementById('noteTitle')
const noteContent = document.querySelector('.note-content')

// 自动保存功能
let saveTimeout
const autoSave = () => {
    clearTimeout(saveTimeout)
    saveTimeout = setTimeout(() => {
        const noteData = {
            title: noteTitle.value,
            content: noteContent.value
        }
        ipcRenderer.send('save-note', noteData)
    }, 500)
}

// 添加输入事件监听
noteTitle.addEventListener('input', autoSave)
noteContent.addEventListener('input', autoSave)

// 关闭便签
function closeNote() {
    // 保存最后的内容
    const noteData = {
        title: noteTitle.value,
        content: noteContent.value
    }
    ipcRenderer.send('save-note', noteData)
    ipcRenderer.send('close-note')
}

// 监听设置更新
ipcRenderer.on('update-settings', (event, settings) => {
    if (settings.fontSize) {
        noteContent.style.fontSize = `${settings.fontSize}px`
    }
})

// 加载便签数据
ipcRenderer.on('load-note', (event, noteData) => {
    if (noteData) {
        noteTitle.value = noteData.title || ''
        noteContent.value = noteData.content || ''
    }
}) 